package myTest;

import org.junit.Test;

import java.util.HashMap;

public class HashTest {

    @Test
    public void hash() {
        String str = "java";
        int origin = str.hashCode();
        System.out.println(origin);
        System.out.println(Integer.toBinaryString(origin));
        System.out.println(origin % 16);
        System.out.println(origin >>> 16);
        System.out.println(Integer.toBinaryString(origin >>> 16));
        System.out.println(origin % 16);
        int h;
        int afterHash = (h = str.hashCode()) ^ (h >>> 16);
        System.out.println(afterHash);
        System.out.println(Integer.toBinaryString(afterHash));
        System.out.println(afterHash % 16);


        System.out.println("Aa".hashCode());
        System.out.println("BB".hashCode());

        System.out.println(ELFHash("Aa"));
        System.out.println(ELFHash("BB"));

        long l1 = Long.valueOf("xxxxxxxxxxxxxx".toLowerCase().hashCode()) + Integer.MAX_VALUE;
        long l2 = Long.valueOf("xxxxxxxxxxxxxx".toLowerCase().hashCode()) << 32;
        System.out.println(l1);
        System.out.println(l2);

        System.out.println("xxxxxxxxxxxxxx".toLowerCase().hashCode() + Integer.MAX_VALUE);
        System.out.println("xxxxxxxxxxxxxx".toLowerCase().hashCode() << 31);

    }

    public static long ELFHash(String strUri) {
        long hash = 0;
        long x = 0;
        for (int i = 0; i < strUri.length(); i++) {
            hash = (hash << 4) + strUri.charAt(i);
            if ((x = hash & 0xF0000000L) != 0) {
                hash ^= (x >> 24);
                hash &= ~x;
            }
        }
        return (hash & 0x7FFFFFFF);
    }
    static HashMap map = new HashMap();

    private static char startChar = 'A';

    private static char endChar = 'z';

    private static int offset = endChar - startChar + 1;

    private static int dup = 0;

    public static void main(String[] args) {
        int len = 3;
        char[] chars = new char[len];
        tryBit(chars, len);
        System.out.println((int)Math.pow(offset, len) + ":" + dup);
    }

    private static void tryBit(char[] chars, int i) {
        for (char j = startChar; j <= endChar; j++) {
            chars[i - 1] = j;
            if (i > 1)
                tryBit(chars, i - 1);
            else
                test(chars);
        }
    }

    private static void test(char[] chars) {

        String str = new String(chars).replaceAll("[^a-zA-Z_]", "").toUpperCase();// 195112:0
        //String str = new String(chars).toLowerCase();//195112:6612
        //String str = new String(chars).replaceAll("[^a-zA-Z_]","");//195112:122500
        //String str = new String(chars);//195112:138510
        int hash = str.hashCode();
        //long hash = ELFHash(str);
        if (map.containsKey(hash)) {
            String s = (String) map.get(hash);
            if (!s.equals(str)) {
                dup++;
                System.out.println(s + ":" + str);
            }
        } else {
            map.put(hash, str);
            // System.out.println(str);
        }
    }

}
